<?php
/**
 * BOE Officials
 * Return elected representatives based on district and qualifier.
 * 
 * @param 		string 		$district_type 		Type of political district
 * @param 		string		$district			Specific district if needed
 * @param 		string		$format			format of output, either json or xml
 * @return 		string						resulting json or xml string
 * @version 	v1.0  15 October 2007
 * @author 		Tobin Bradley
 * @license 	http://opensource.org/licenses/gpl-license.php GNU Public License
 * @package 	ws_boe_officials.php
 */

# Includes
require_once("xml_regex.inc.php");
require_once("../mn_ini.php");

# Retrive URL arguments
try {
	$search = $_REQUEST['search'];
	$field = $_REQUEST['field'];
	$format = $_REQUEST['format'];
} 
catch (Exception $e) {
    echo ("Caught Exception: " . $e->getMessage() . E_USER_ERROR);
}

# Performs the query and returns XML or JSON
try {
	if ($format == "json" || $format == "xml") {
		# Open folder
		$dir= opendir($folder);
		
		
		$xmlresponse = ""; 
		$jsonresponse = "";
		$i = 0;
		while($file=readdir($dir)){
			if ($file!="." and $file!="..") {
				$thepath = $dir . "/" . $file;
				$xml = file_get_contents($folder . $file);
				$searchfield = $xml;
				if (strlen($field) > 0) $searchfield = value_in($field, $xml);
				$title = value_in('title', $xml);
				$abstract = value_in('abstract', $xml);
				if(stristr($searchfield, $search)) {
					$jsonresponse .= '{"row":{"title":"' . urlencode($title) . '", "abstract":"' . urlencode($abstract) . '", "file_name":"' . urlencode($file) . '"}},';		
					$xmlresponse .= '<row><column name="title">' . $title . '</column><column name="abstract">' . $abstract . '</column><column name="file_name">' . $file . '</column></row>';
					$i++;
				}
			}
		}
		$jsonresponse = rtrim(rtrim($jsonresponse, ','));
		if ($i > 1) {
			$xmlresponse .= "</rows>";
			$xmlresponse = '<rows total-rows="' . $i . '">' . $xmlresponse;
			$jsonresponse .= "]}";
			$jsonresponse = '{"total_rows":"' . $i . '","rows":[' . $jsonresponse;
		}
		else {			
			$xmlresponse =  '<rows total-rows="0"/>';
			$jsonresponse =  '{"total_rows":"0","rows":"row"}';
		}
		
		
		if ($format == 'xml') {
			header("Content-Type: text/xml");
			echo $xmlresponse;
		}
		
		elseif ($format == 'json') {
			header("Content-Type: application/json");
			if (isset($_REQUEST['callback'])) $jsonresponse = $_REQUEST['callback'] . '(' . $jsonresponse . ')';
			echo $jsonresponse;
		}
		else {
			trigger_error("Caught Exception: format must be xml or json.", E_USER_ERROR);
		}
	}
	elseif ($format == "raw") {
		$xml = file_get_contents($folder . $search);
		header("Content-Type: text/xml");
		echo $xml;
	}
	elseif ($format == "classic") {
		
		header("Content-Type: text/html");
		$xml = new DomDocument();
		$xml->load($folder . $search);
																					   
		$xsl = new DomDocument;
		$xsl->load('FGDC_Classic_for_Web_body.xsl');
																					   
		$proc = new xsltprocessor();
		$proc->importStyleSheet($xsl);
		$results = $proc->transformToXML($xml);
		if (file_exists($folder . $search . ".png")) $results = '<img src="metadata/' . $search . '.png" width="200" class="center" style="display: block; margin-left: auto; margin-right: auto;" />' . $results;
		
		echo($results);

	}
	elseif ($format == "faq") {
		
		header("Content-Type: text/html");
		$xml = new DomDocument();
		$xml->load($folder . $search);
																					   
		$xsl = new DomDocument;
		$xsl->load('FGDC_FAQ_for_Web_body.xsl');
																					   
		$proc = new xsltprocessor();
		$proc->importStyleSheet($xsl);
		$results = $proc->transformToXML($xml);
		if (file_exists($folder . $search . ".png")) $results = '<img src="metadata/' . $search . '.png" width="200" class="center" style="display: block; margin-left: auto; margin-right: auto;" />' . $results;
		
		echo($results);

	}
	elseif ($format == "summary") {
		
		header("Content-Type: text/html");
		$xml = new DomDocument();
		$xml->load($folder . $search);
																	   
		$xsl = new DomDocument;
		$xsl->load('FGDC_Summary.xsl');
																					   
		$proc = new xsltprocessor();
		$proc->importStyleSheet($xsl);
		$results = $proc->transformToXML($xml);
		if (file_exists($folder . $search . ".png")) $results = '<img src="metadata/' . $search . '.png" width="200" class="center" style="display: block; margin-left: auto; margin-right: auto;" />' . $results;
		
		echo($results);

	}
}
catch (Exception $e) {
	echo ("Caught Exception: " . $e->getMessage() . E_USER_ERROR);
}


?>